$(function() {
	//获取缓存中的变量
	var pid = localStorage.getItem("pid");
	// 取出商品列表界面传过来的参数  
	function GetRequest() {
		var url = location.search;
		var theRequest = new Object();
		if(url.indexOf("?") != -1) {
			var str = url.substr(1);
			strs = str.split("&");
			for(var i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
			}
		}
		return theRequest;
	}
	//获取地址栏中的路径，判断是否有问号，有参数
	var url = window.location.href;
	if(url.indexOf("?") != -1) {
		var id = GetRequest().id;
		loadSwiper(id);
		ajax(id);
	} else {
		loadSwiper(pid);
		ajax(pid);
	}
	//展示轮播图，时间
	function loadSwiper(id) {
		$.ajax({
			type: "post",
			url: "http://a.91jfk.com/Product/Indiana/details",
			async: true,
			data: {
				id: id
			},
			dataType: 'json',
			success: function(res) {
				if(res.code == '1') {
					console.log(res);
					//设置轮播图
					var msg = res.msg;
					var arr = [];
					var imgArr = [];
					arr = msg.piclist.split(",");
					for(var i = 0; i < arr.length; i++) {
						imgArr.push(arr[i]);
					}
					// 轮播图
					var Swiper = new SwiperLunBo();
					Swiper.carousel(imgArr).swiper().changeLunBo(imgArr); //changeLunBo方法只能在swiperLunBo方法之后调用

					$(".title p").text(msg.pname);
					$("#time").text(getLocalTime(msg.starttime));
					$("#price").text(msg.price);
					$("#pimg").attr("src",msg.thumbnail);
					var storage = window.localStorage;
					storage.setItem("db_stock",msg.stock);
				} else {

				}
			},
			error: function(err) {
				console.log(err);
			}
		});
	}
	//展示获奖者和所有参与者的信息
	function ajax(id) {
		$.ajax({
			type: "post",
			url: "http://a.91jfk.com/Product/Order/goods_detail",
			async: true,
			data: {
				id: id
			},
			dataType: 'json',
			success: function(res) {
				console.log(res);
				if(res.state = "2") {
					$(".not_open").show();
					$(".jflg_content .details_announced .detail").hide();
				} else if(res.state == "4") {
					$(".not_open").hide();
					$(".jflg_content .details_announced .detail").show();
					var qishu = getDate(res.qishu - 0);
					$('.period').children("span:nth-child(2)").text(qishu);
					goods_luck(res.luck_user.pic, res.luck_user.nickname, res.luck_user.id, res.luck_user.buy_num, res.luck_user.luck_time);
					for(var i = 0; i < res.all_order.length; i++) {
						var time = format(res.all_order[i].time - 0);
						all_order(res.all_order[i].pic, res.all_order[i].name, res.all_order[i].num, time);
					}
				}

			},
			error: function(err) {
				console.log(err);
			}
		});
	}

	//回到顶部
	$(window).scroll(function() {
		var sc = $(window).scrollTop();
		if(sc > 200) {
			$(".goTop").css("display", "block");
		} else {
			$(".goTop").css("display", "none");
		}
	});
	$(".goTop").click(function() {
		var sc = $(window).scrollTop();
		$('body,html').animate({
			scrollTop: 0
		}, 500);
	});
	//将时间戳转化为具体日期 
	function getLocalTime(nS) {
		return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
	}
	//将时间戳转化为以-相隔的年月日时分秒
	function add0(m) {
		return m < 10 ? '0' + m : m
	}

	function format(shijianchuo) {
		//shijianchuo是整数，否则要parseInt转换
		var time = new Date(shijianchuo);
		var y = time.getFullYear();
		var m = time.getMonth() + 1;
		var d = time.getDate();
		var h = time.getHours();
		var mm = time.getMinutes();
		var s = time.getSeconds();
		return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
	}
	//获奖者信息
	function goods_luck(src, name, id, num, time) {
		$(".tou").attr("src", src);
		var right = $(".prize_winner .right");
		var content = $('<p>获得者&nbsp;:&nbsp;' + name + '</p><p>用户ID&nbsp;:&nbsp;' + id + '(唯一不变标识)</p><p>本期下注&nbsp;:&nbsp;' + num + '注</p><p>揭晓时间&nbsp;:&nbsp;' + time + '</p>');
		right.append(content);
	}
	//显示所有的参与者
	function all_order(src, name, num, time) {
		var record_detail = $(".record_detail");
		var list = $('<list class="list"></list>');
		var content = $('<div class="left"><img style="width: 0.8rem;height:0.8rem;" src="' + src + '"></div><div class="right"><p>' + name + '</p><p><span>押了' + num + '注</span><span>' + time + '</span></p></div>');
		list.append(content)
		record_detail.append(list);
	}
	//点击往期揭晓跳转页面
	$(".announce").click(function() {
		window.location.href = "page_wangqijiexiao.html";
	});

	//点击分享显示图标
	$(".jflg_content .title img").click(function() {
		$(".mask").show();
		$(".prompt").show();
		$("body").css("overflow-y", "hidden");
	});
	//点击X进行关闭
	$(".prompt>p img").click(function() {
		$(".prompt").hide();
		$(".mask").hide();
		$("body").css("overflow-y", "auto");
	});

	//点击立即夺宝，号码池出现
	$(".jflg_bottom").click(function() {
		$(".buy").show();
		$(".buy_num").show();
		$("this").hide();
		$(".mask").show();
		$("body").css("overflow-y", "hidden");
		if($("li").length >= 5) {
			console.log($(".add"));
			$(".add").css("background", "#d1d1d1");
			$(".type:nth-child(4)").unbind("click");
		}
	});
	//点击关闭，号码池消失
	$(".buy_header img").click(function() {
		$(".buy").hide();
		$(".buy_num").hide();
		$("this").show();
		$(".mask").hide();
		$("body").css("overflow-y", "auto");
	});
	//点击减
	$("#reduce").click(function() {
		var num = $("#change_num").text() - 0;
		if(num > 1) {
			num--;
		}
		$("#change_num").text(num);
		$(".num span").text(num);
		$("#n").text(num);
		$(".red_num").text(num * 5);
		if(num == 1) {
			$("#change_num").text(num);
			$(".num span").text(num);
			$("#n").text(num);
			$(".red_num").text(num * 5);
		}
	});
	//点击加
	$("#plus").click(function() {
		var num = $("#change_num").text() - 0;
		num++;
		$("#change_num").text(num);
		$(".num span").text(num);
		$("#n").text(num);
		$(".red_num").text(num * 5);
		
		var stock = localStorage.getItem("db_stock") - 0;
		if(num == stock){
			$("#plus").unbind("click");
		}
	});

	//点击确认支付进入确认订单页
	$(".confirm_payment").click(function() {
		var num = $("#change_num").text();
		var id = pid;
		var oimg = $("#pimg").attr("src");
		var pname = $(".title p").text();
		var price = $("#price").text();
		console.log(id);
		var storage = window.localStorage;
		var obj = {
			name: pname,
			id: id,
			num: num,
			oimg: oimg,
			price: price
		}
		var d = JSON.stringify(obj);
		console.log(d);
		storage.setItem("indiana", d);
		window.location.href = "../Zero/indiana_order.html";
	});

	//将时间戳转化为具体具体时间，中间不带空格和横线的,解决商品期数的时间戳
	function getDate(shijianchuo) {
		//shijianchuo是整数，否则要parseInt转换  
		var time = new Date(shijianchuo);
		var y = time.getFullYear();
		var m = time.getMonth() + 1;
		var d = time.getDate();
		var h = time.getHours();
		var mm = time.getMinutes();
		var s = time.getSeconds();
		return y + '' + add0(m) + '' + add0(d) + '' + add0(h) + '' + add0(mm) + '' + add0(s);
	};

});